package finedev.android.venue;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class Detail_NewVideoActivity extends Activity {
	
	private static final String TAG = "newvideo";
	private Activity mActivity = null;
	private VideoItem selectedItem = null;
	
	ImageButton imgbtn_video = null;
	Button btn_intro = null;
	Button btn_relateVideo = null;
	Button btn_comment = null;
	
	Button btn_fbFeed = null;
	
	LinearLayout frameTab1;
	LinearLayout frameTab2;
	LinearLayout frameTab3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail__new_video);
        
        Bundle bundle = getIntent().getExtras();
        selectedItem = bundle.getParcelable("item");
        
        mActivity = this;
        
        imgbtn_video = (ImageButton) findViewById(R.id.detail_newvideo_imgbtn_video );
		try {
			URL url = new URL( selectedItem.getThumnail() );
			Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
			imgbtn_video.setImageBitmap( bitmap );
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
        btn_intro = (Button) findViewById(R.id.detail_newvideo_btn_introduction );
        btn_relateVideo = (Button) findViewById(R.id.detail_newvideo_btn_relatedvideo );
        btn_comment = (Button) findViewById(R.id.detail_newvideo_btn_comment );
        
        btn_fbFeed = (Button) findViewById(R.id.fbtest);
        btn_fbFeed.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Bundle params = new Bundle();
				params.putString("picture", selectedItem.getThumnail() );
				params.putString("name", selectedItem.getTitle() );				
				params.putString("link","http://vennue.net/new/posted_ad.php?user_key="+BasicInfo.USER_KEY+"&adNo="+selectedItem.getAdKey());
				BasicInfo.FacebookInstance.dialog( mActivity , "feed", params, new SampleDialogListener() );
			}
		});
        
        frameTab1 = (LinearLayout) findViewById(R.id.frameTab1);
        frameTab2 = (LinearLayout) findViewById(R.id.frameTab2);
        
        btn_intro.setOnClickListener( tabOnClickListener );
        btn_relateVideo.setOnClickListener( tabOnClickListener );
        btn_comment.setOnClickListener( tabOnClickListener );
    }
    
    public class SampleDialogListener extends BaseDialogListener {

        public void onComplete(Bundle values) {
            final String postId = values.getString("post_id");
            if (postId != null) {
                Log.d( TAG, "Dialog Success! post_id=" + postId);            } else {
                Log.d( TAG, "No wall post made");
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_detail__new_video, menu);
        return true;
    }
    
    private OnClickListener tabOnClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			frameTab1.setVisibility(View.INVISIBLE);
			frameTab2.setVisibility(View.INVISIBLE);
			if ( v.getId() == R.id.detail_newvideo_btn_introduction ) {
				frameTab1.setVisibility(View.VISIBLE);
			} else if ( v.getId() == R.id.detail_newvideo_btn_relatedvideo ) {
				frameTab2.setVisibility(View.VISIBLE);
			} else if ( v.getId() == R.id.detail_newvideo_btn_comment ) {
				
			}
			
		}
	};
}
